package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品 {}",dishDTO);
        dishService.saveWithFlavor(dishDTO);

        deleteCache("dish_"+dishDTO.getCategoryId());

        return Result.success();
    }

    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result page(DishPageQueryDTO dishPageQuery){
        log.info("菜品分页查询 参数{}",dishPageQuery);
        PageResult pageResult = dishService.page(dishPageQuery);

        return Result.success(pageResult);
    }
    /**
     * 根据分类id查询菜品
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result list(Integer categoryId){
        log.info("根据分类id查询菜品 categoryId={}",categoryId);
        List<Dish> dishList = dishService.getByCategoryId(categoryId);

        return Result.success(dishList);
    }
    /**
     * 批量删除菜品
     */
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result deleteByIds(@RequestParam List<Long> ids){
        log.info("根据ids批量删除菜品 ids = {}",ids);
        dishService.deleteBatch(ids);

        deleteCache("dish_*");

        return Result.success();
    }

    /**
     * 根据id查询菜品
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品信息")
    public Result getById(@PathVariable Long id){
        log.info("根据id查询菜品及菜品口味信息 id= {}",id);
        DishVO dishVO = dishService.getById(id);

        return Result.success(dishVO);
    }

    /**
     * 修改菜品信息
     */
    @PutMapping
    @ApiOperation("修改菜品信息")
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品信息，参数={}",dishDTO);
        dishService.updateWithFlavors(dishDTO);

        deleteCache("dish_*");

        return Result.success();
    }
    /**
     * 修改菜品状态
     */
    //TODO 修改菜品状态完善
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("修改菜品为{}",status == 1 ? "起售" : "停售");
        DishDTO dish = new DishDTO();
        dish.setStatus(status);
        dish.setId(id);
        dishService.updateWithFlavors(dish);

        deleteCache("dish_*");

        return Result.success();
    }

    private void deleteCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}
